package com.xhwl.data.mq.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

/**
 * @Author: JCccc
 * @Description: 将账号数据推送到主题
 * @Date: Create in 17:23 2021/06/15
 */
@Component
public class AccountProducer {


    private static String CREATE_TOPIC="cent-data-account-create";
    private static String UPDATE_TOPIC="cent-data-account-update";
    private static String DELETE_TOPIC="cent-data-account-delete";
    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;
    private final Logger log = LoggerFactory.getLogger(AccountProducer.class);

    public void createPush( String jsonStr) {
        //发送消息 send 方法可以指定分区或者不指定分区
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(CREATE_TOPIC,jsonStr);
        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            //推送成功
            public void onSuccess(SendResult<String, Object> result) {
                log.info("主题={}， 账号创建数据推送成功={}",CREATE_TOPIC,jsonStr);
            }
            @Override
            //推送失败
            public void onFailure(Throwable ex) {
                log.info("主题={}， 账号创建数据推送失败={}",CREATE_TOPIC,jsonStr);
            }
        });
    }


    public void updatePush( String jsonStr) {
        //发送消息 send 方法可以指定分区或者不指定分区
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(UPDATE_TOPIC,jsonStr);
        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            //推送成功
            public void onSuccess(SendResult<String, Object> result) {
                log.info("主题={}， 账号更新数据推送成功={}",UPDATE_TOPIC,jsonStr);
            }
            @Override
            //推送失败
            public void onFailure(Throwable ex) {
                log.info("主题={}， 账号更新数据推送失败={}",UPDATE_TOPIC,jsonStr);
            }
        });
    }


    public void deletePush( String jsonStr) {
        //发送消息 send 方法可以指定分区或者不指定分区
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(DELETE_TOPIC,jsonStr);
        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            //推送成功
            public void onSuccess(SendResult<String, Object> result) {
                log.info("主题={}， 账号删除数据推送成功={}",DELETE_TOPIC,jsonStr);
            }
            @Override
            //推送失败
            public void onFailure(Throwable ex) {
                log.info("主题={}， 账号删除数据推送失败={}",DELETE_TOPIC,jsonStr);
            }
        });
    }







}